//
//  MyCardVC.m
//  WanCaiC8
//
//  Created by tangang on 2018/3/20.
//  Copyright © 2018年 tangang. All rights reserved.
//

#import "MyCardVC.h"
#import "MyCardCell.h"
#import "InvitationRegisterVC.h"
#import "MyCardRulesVC.h"
#import "MyVM.h"
#import "MJRefresh.h"
@interface MyCardVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic ,strong) UITableView   *tableView;
@property (nonatomic ,strong) UIButton      *btnDown;
@property (nonatomic ,strong) NSMutableArray *dataArr;
@end

@implementation MyCardVC{
    UIView *lineView;
    UIButton *btnNoUse;
    UIButton *btnUse;
    UIButton *btnFailure;
    NSInteger type;///0 未使用 1已使用 2已过期
    NSInteger page;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"我的卡券";
    UIBarButtonItem *rightItem=[[UIBarButtonItem alloc]initWithTitle:@"规则" style:UIBarButtonItemStylePlain target:self action:@selector(onclickRules)];
    self.navigationItem.rightBarButtonItem=rightItem;
    [self.tableView.header beginRefreshing];
    [self headView];
}

- (void)getData{
    WS(weakSelf);
    [[MyVM manage] getCardWithType:type andPage:page andSuccess:^(id returnValue) {
        NSArray *arr=returnValue;
        if (arr.count<[defaultPageSize integerValue]) {
            [weakSelf.tableView.footer noticeNoMoreData];
        }else{
            [weakSelf.tableView.footer endRefreshing];
        }
        [weakSelf.tableView.header endRefreshing];
        [weakSelf.dataArr addObjectsFromArray:returnValue];
        [weakSelf.tableView reloadData];
    } andFail:^(NSString *errorMessage, NSError *error) {
        [weakSelf.tableView.header endRefreshing];
        [weakSelf.tableView.footer endRefreshing];
    }];
}


- (void)headView{
    WS(weakSelf);
    UIView *topView=[[UIView alloc]init];
    topView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:topView];
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(weakSelf.view);
        make.height.mas_equalTo(@44);
    }];
    
    btnNoUse=[self createBtnWithTitle:@"未使用"];
    btnNoUse.tag=0;
    [topView addSubview:btnNoUse];
    [btnNoUse mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.equalTo(topView);
        make.width.equalTo(topView).with.multipliedBy(1/3.0);
    }];
    
    btnUse=[self createBtnWithTitle:@"已使用"];
    btnUse.tag=1;
    [topView addSubview:btnUse];
    [btnUse mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(topView);
        make.centerX.equalTo(topView);
        make.width.equalTo(topView).with.multipliedBy(1/3.0);
    }];
    
    btnFailure=[self createBtnWithTitle:@"已过期"];
    btnFailure.tag=2;
    [topView addSubview:btnFailure];
    [btnFailure mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(topView);
        make.right.equalTo(topView);
        make.width.equalTo(topView).with.multipliedBy(1/3.0);
    }];
    
    lineView=[[UIView alloc]init];
    lineView.backgroundColor=NAVCOLOR;
    [topView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(btnNoUse);
        make.bottom.equalTo(topView);
        make.size.mas_equalTo(CGSizeMake(44, 2));
    }];
}

- (UIButton *)createBtnWithTitle:(NSString *)title{
    UIButton *btn=[[UIButton alloc]init];
    [btn addTarget:self action:@selector(onclickTop:) forControlEvents:UIControlEventTouchUpInside];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:DEFAULTTEXTGDARKRAYCOLOR forState:UIControlStateNormal];
    [btn.titleLabel setFont:[UIFont systemFontOfSize:15]];
    return btn;
}


- (void)onclickTop:(UIButton *)sender{
    type=sender.tag;
    [self.tableView.header beginRefreshing];
    if (sender.tag==0) {
        [lineView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(sender);
        }];
    }else if (sender.tag==1){
        [lineView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(btnNoUse).with.offset(SCREEN_WIDTH/3.0);
        }];
    }else if (sender.tag==2){
        [lineView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(btnNoUse).with.offset(SCREEN_WIDTH/3.0*2);
        }];
    }
}

#pragma mark -- 查看规则
- (void)onclickRules{
    MyCardRulesVC *vc=[[MyCardRulesVC alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark -- 邀请好友
- (void)onclickInvitation{
    InvitationRegisterVC *vc=[[InvitationRegisterVC alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark -- UITableViewDelegate ,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIImage *image=[UIImage imageNamed:@"effective"];
    return image.size.height/(image.size.width/(SCREEN_WIDTH-20))+10;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 5;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyCardCell *cell=[tableView dequeueReusableCellWithIdentifier:@"MyCardCell" forIndexPath:indexPath];
    cell.status=type;
    return cell;
}

#pragma mark -- 懒加载
- (UITableView *)tableView{
    if (!_tableView) {
        WS(weakSelf);
        _tableView=[[UITableView alloc]init];
        _tableView.dataSource=self;
        _tableView.delegate=self;
        _tableView.backgroundColor=DEFAULTBGCOLOR;
        _tableView.header=[MJRefreshNormalHeader headerWithRefreshingBlock:^{
            page=1;
            [weakSelf.dataArr removeAllObjects];
            [weakSelf.tableView reloadData];
            [weakSelf getData];
        }];
        _tableView.footer=[MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            [weakSelf getData];
        }];
        [_tableView registerClass:[MyCardCell class] forCellReuseIdentifier:@"MyCardCell"];
        _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        [self.view addSubview:_tableView];
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakSelf.view).with.offset(44);
            make.left.right.equalTo(weakSelf.view);
            make.bottom.equalTo(weakSelf.btnDown.mas_top);
        }];
    }
    return _tableView;
}

- (UIButton *)btnDown{
    if (!_btnDown) {
        WS(weakSelf);
        _btnDown=[[UIButton alloc]init];
        _btnDown.backgroundColor=[UIColor whiteColor];
        _btnDown.layer.shadowColor=DEFAULTLINECOLOR.CGColor;
        _btnDown.layer.shadowOffset=CGSizeMake(0, 0);
        _btnDown.layer.shadowOpacity=0.5f;
        [_btnDown addTarget:self action:@selector(onclickInvitation) forControlEvents:UIControlEventTouchUpInside];
        [_btnDown setTitle:@"邀请好友注册" forState:UIControlStateNormal];
        [_btnDown setTitleColor:NAVCOLOR forState:UIControlStateNormal];
        [self.view addSubview:_btnDown];
        [_btnDown mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(weakSelf.view);
            make.height.equalTo(weakSelf.view).with.multipliedBy(.1);
        }];
    }
    return _btnDown;
}

- (NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr=[[NSMutableArray alloc]init];
    }
    return _dataArr;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
